package com.lcy.web;

import com.lcy.web.resource.StringResource;
import com.lcy.web.resource.XmlResource;
import com.lcy.web.utils.CommonUtils;

/**
 * Created by loucunyin on 2016/11/26 0026.
 * 用于获取常量
 */
public class Const {
    public static final String HOST = "localhost";
    public static final String PORT = "3306";
    public static final String DATABASE = "my_server";

    private static final String NAME_KEY = "123456789ABCDEF0";
    private static final String PASSWORD_KEY = "fedcba9876543210";

    private static final String SQL_CONFIG = "res/config/mysql";
    private static final String STRING = "res/string";

    /**
     * 获取 jdbc url
     *
     * @return mysql jdbc url
     */
    public static String getDbUrl() {
        String template = "jdbc:mysql://%s:%s/%s?useSSL=true&characterEncoding=UTF-8&serverTimezone=UTC";
        String host = DbConfig.sResource.getString("host", HOST);
        String port = DbConfig.sResource.getString("port", PORT);
        String dbName = DbConfig.sResource.getString("database", DATABASE);
        return String.format(template, host, port, dbName);
    }

    public static String getDbName(){
        return DbConfig.sResource.getString("database", DATABASE);
    }

    /**
     * 获取数据库的用户名
     * 判断是否经过 base64 加密，加密的返回解密数据
     *
     * @return 用户名
     */
    public static String getDbUser() {
        String user = DbConfig.sResource.getString("user");
        if (user.endsWith("==")) {
            user = CommonUtils.decrypt(user, NAME_KEY);
        }
        return user;
    }

    /**
     * 获取数据库密码
     * 判断是否经过 base64 加密，加密的返回解密数据
     *
     * @return 密码
     */
    public static String getDbPassword() {
        String password = DbConfig.sResource.getString("password");
        if (password.endsWith("==")) {
            password = CommonUtils.decrypt(password, PASSWORD_KEY);
        }
        return password;
    }

    /**
     * 获取 res/string.properties 内的 string
     *
     * @param key id
     * @return string
     */
    public static String getString(String key) {
        return StringRes.sResource.getString(key);
    }

    /**
     * 获取 res/string.properties 内的 string
     * string 不存在返回 defaultValue
     *
     * @param key key
     * @param defaultValue 默认值
     * @return string
     */
    public static String getString(String key, String defaultValue) {
        return StringRes.sResource.getString(key, defaultValue);
    }

    /**
     * 获取 res/sql/sql.xml 中的 sql
     * @param id id
     * @return sql
     */
    public static String getSql(String id) {
        return SqlResource.getSql(id);
    }

    private static class StringRes {
        private static final StringResource sResource = new StringResource(STRING);
    }

    public static class SqlResource extends XmlResource {
        private static SqlResource sSqlResource = new SqlResource();

        @Override
        protected String getXmlPath() {
            return "res/sql/sql.xml";
        }

        private static String getSql(String id) {
            return sSqlResource.getResById(id);
        }
    }

    private static class DbConfig {
        private static final StringResource sResource = new StringResource(SQL_CONFIG);
    }

}
